<template>
  <div class="app-container">
    <div class="ele-table-tool-default flex">
      <el-date-picker
        v-model="searchData.gdate"
        type="daterange"
        range-separator=" ~ "
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="yyyy-MM-dd"
        clearable
        class="mr12"
      />
      <el-button type="primary">查询</el-button>
      <el-button type="success">导出</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        prop="date"
        label="序号"
        width="60"
        align="center"
      />
      <el-table-column
        prop="account"
        label="账户"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="money"
        label="设备"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="thumb"
        label="开始表底"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="created_at"
        label="结束表底"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="created_at"
        label="取水量"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="created_at"
        label="金额"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="created_at"
        label="取水时间"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        prop="price"
        label="操作"
        align="center"
        width="160"
      >
        <template #default="scope">
          <el-link type="primary" icon="el-icon-edit" class="mr12" @click="edit(scope.row)">修改</el-link>
          <el-link type="success" icon="el-icon-key" class="mr12" @click="edit(scope.row)">审核</el-link>
          <el-link type="danger" icon="el-icon-delete" @click="del(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getAll, addOne, updateOne, deleteOne } from '@/api/waterUsed'
import { removeEmptyField } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      tableData: [],
      row: null,
      params: { status: '' },
      searchData: {}
    }
  },
  computed: {
    ...mapGetters(['project'])
  },
  created() {
    this.getAll()
  },
  methods: {
    showForm() {
      this.$refs.mainForm.show()
    },
    async getAll() {
      this.params.project_id = this.project.id
      const { data } = await getAll(removeEmptyField(this.params))
      this.tableData = data.data
    },
    submitForm(formData) {
      if (formData.id) {
        this.updateOne(formData)
      } else {
        this.addOne(formData)
      }
      this.$refs.mainForm.hide()
    },
    async addOne(formData) {
      formData.project_id = this.project.id
      const { msg } = await addOne(formData)
      this.$message.success(msg)
      this.getAll()
    },
    async updateOne(formData) {
      const { msg } = await updateOne(formData.id, formData)
      this.$message.success(msg)
      this.getAll()
    },
    async deleteOne(id) {
      const { msg } = await deleteOne(id)
      this.$message.success(msg)
      this.getAll()
    },
    edit(row) {
      this.$refs.mainForm.show(row)
    },
    del(row) {
      this.$confirm('确定要删除此条记录吗？', '操作确认', { type: 'warning' }).then(() => {
        this.deleteOne(row.id)
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
